package cn.lyjuan.android.mylibrary.frame.load.impl;

import android.app.Activity;
import android.app.Fragment;
import android.os.Build;

import java.util.ArrayList;
import java.util.List;

import cn.lyjuan.android.mylibrary.frame.load.IFragmentLoad;
import cn.lyjuan.android.mylibrary.frame.load.fragment.AbstractLoadFailedFragment;
import cn.lyjuan.android.mylibrary.util.PermissionUtils;

/**
 * 提供的一种网络视图实现
 * Created by chad on 12/11/2017.
 */
public class FragmentLoadImpl extends AbstractLoad
{
    protected List<AbstractLoad> loads;

    @Override
    public boolean shouldNormal()
    {
        return true;
    }

    @Override
    public AbstractLoadFailedFragment failedFragment()
    {
        throw new IllegalArgumentException("cannot invoke this method");
    }

    public FragmentLoadImpl(Activity context, Fragment normal, int containerId)
    {
        super(context, normal, containerId);
    }

    @Override
    public List<String> enforcePermission()
    {
        if (null == loads || loads.isEmpty()) return null;

        List<String> list = new ArrayList<>();

        for (IFragmentLoad l : loads)
        {
            if (null != l.enforcePermission())
                list.addAll(l.enforcePermission());
        }

        return list;
    }

    @Override
    public List<String> recomPermission()
    {
        if (null == loads || loads.isEmpty()) return null;

        List<String> list = new ArrayList<>();

        for (IFragmentLoad l : loads)
        {
            if (null != l.recomPermission())
                list.addAll(l.enforcePermission());
        }

        return list;
    }

    /**
     * 授权
     */
    private void grant()
    {
        if (Build.VERSION.SDK_INT < 23)
            return;

        // 强制权限
        List<String> enforces = enforcePermission();
        // 建议权限
        List<String> recoms = recomPermission();

        // 授权
        PermissionUtils.grant(context, enforces, recoms);
    }

    @Override
    public void inflate()
    {
        Fragment fragment = normalFragment();// 默认显示正常页面

        if (null == loads || loads.isEmpty())
        {
            replace(fragment);
            return;
        }

        // 先授权
        grant();

        for (AbstractLoad a : loads)
        {
            boolean shouldNormal = a.shouldNormal();
            if (shouldNormal) continue;

            fragment = a.failedFragment().setOnReloadListener(this);

            break;// 第一个状态时停止
        }

        Fragment exist = fm.findFragmentByTag(fragmentTag());

        if (exist == fragment)
            return;// 同样的fragment不重新加载

        replace(fragment);
    }

    private void replace(Fragment fragment)
    {
        fm
                .beginTransaction()
                .replace(containerId(), fragment, fragmentTag())
                .commit();
    }

    /**
     * 增加检测
     *
     * @param loads
     */
    public void addLoads(AbstractLoad... loads)
    {
        if (null == loads || loads.length < 1)
            return;

        if (null == this.loads)
            this.loads = new ArrayList<>();

        for (AbstractLoad a : loads)
            this.loads.add(a);
    }
}
